from pythonwork.photo.RequestUtil import RequestUtil
from pythonwork.photo.ThreadDown import ThreadDown
from bs4 import BeautifulSoup


class ParseUntil:

    @staticmethod
    def parse_qiaotu(urls):
        response = RequestUtil.get_request(urls, 'text')

        #qiaotu 首页解析
        soup = BeautifulSoup(response)
        a_tag = soup.select(".thumb a")
        set_url = "http://www.qiaotu.org"
        pic_list = []
        for info in a_tag:
            href = set_url + info["href"]
            pic_list.append(href)
            print(href)
        print("首页解析结束->返回首页链接")
        return pic_list

    @staticmethod
    def parse_detail_qiaotu(url_list):
        response = RequestUtil.get_list_request(url_list, 'text')
        print("详情页解析开始")
        all_list = []
        for res in response:
            soup = BeautifulSoup(res)
            a_tag = soup.select(".loop-pagination a")
            set_url = "http://www.qiaotu.org"
            pic_list = []
            for info in range(len(a_tag) - 1):
                href = set_url + a_tag[info]["href"]
                pic_list.append(href)
                print("详情页链接" + href)
            print("详情页解析结束->返回详情页链接")
            all_list.append(pic_list)
        return all_list

    @staticmethod
    def get_pic(url_list):
        print("详情页解析开始")
        all_list = []
        for urls in url_list:
            response_list = RequestUtil.get_list_request(urls, 'text')
            pic_list = []
            for res in response_list:
                soup = BeautifulSoup(res)
                a_tag = soup.select(".centralnav img")
                set_url = "http://www.qiaotu.org"
                for info in a_tag:
                    href = info["src"]
                    pic_list.append(href)
                    print("图片链接" + href)
            all_list.append(pic_list)
        return all_list

if __name__ == "__main__":
    url = "http://www.qiaotu.org/e/action/ListInfo.php?classid=34&tempid=11&orderby=diggtop&line=16"
    pic_list = ParseUntil.parse_qiaotu(url)
    detail_list = ParseUntil.parse_detail_qiaotu(pic_list)
    pic_list = ParseUntil.get_pic(detail_list)
    for pic in pic_list:
        thread = ThreadDown(pic, '1')
        thread.start()
